package 程序员面试金典;

public class 下一个数 {
    public int[] findClosedNumbers(int num) {
        if (num == 1)
            return new int[]{2, -1};
        if (num == 2147483647)
            return new int[]{-1,-1};

        int MAX = nextMax(num);
        int MIN = ~nextMax(~num);
        if (MAX < 0) MAX = -1;
        return new int[]{MAX, MIN};
    }
    int nextMax(long num) {
        long  x  = num & -num;
        long  y = x + num;
        return (int)((num & ~y)/x >> 1 | y);
    }
}
